package com.cqut.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;
import com.cqut.dto.ComParamDTO;
import com.cqut.dto.LimitShowDTO;
import com.cqut.service.IComParamService;
import com.cqut.model.ComParam;
import com.cqut.util.ResponseUtil;
import com.cqut.util.StringUtil;



@RestController
@RequestMapping("/api/comParam")
public class ComParamController {
    @Resource(name="comParamService")
    private IComParamService comParamService;

    /**
     * 初始化模块列表
     * @param limitShowDTO
     * @param errors
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseEntity<String> list(
    	@RequestParam("comParamName")
    	String comParamName,
    	@RequestParam("parentId")
    	String parentId,
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Map<String, Object> result = new HashMap<String, Object>();
        
        List<Map<String, Object>> comParamList = comParamService.getComParamList(comParamName,parentId, limitShowDTO);
        
        long count = comParamService.getComParamListCount(parentId,comParamName);
        
        System.out.println("读取数据库的列表："+comParamList);
        
        result.put("rows", comParamList);
        result.put("total", count);
        System.out.println("一共："+count+"条");
        return ResponseUtil.getResEntityForGetAndJson(result);
    }
    
    /**
     * 新增竞赛参数
     * @param comParamDTO
     * @param errors
     * @return
     */
    
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResponseEntity<String> create(
        @Validated
        ComParamDTO comParamDTO, BindingResult errors) {
        
    	if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();
        
       return ResponseUtil.getResEntityForGet(comParamService.addTableComParam(comParamDTO));
    }
    
    /**
     * 根据id更新竞赛参数信息
     * @param comParamId
     * @param comParamDTO
     * @param errors
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public ResponseEntity<String> update(
    	ComParamDTO comParamDTO,BindingResult errors) {
    	if(errors.hasErrors())
    		return ResponseUtil.getResponseEntityWhenInvalidReqParams();
    	ComParam comParamModel = comParamDTO.toModel();
        return ResponseUtil.getResEntityForPPP(comParamService.update(comParamModel));
    }
    
    /**
	 * 判断竞赛名称是否重复(true为重复，false为不重复)
	 * */
	@RequestMapping(value = "/comParamTableNameDC", method=RequestMethod.POST)
	public ResponseEntity<String> duplicateCheckingComParamName(
			String validationDate){
		
		if (StringUtil.isNullOrEmpty(validationDate))
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		
		JSONObject json = new JSONObject();
		json.put("state", !comParamService.duplicateCheckingComParamName(validationDate));
		
		return ResponseUtil.getResEntityForGet(json.toJSONString());
	}
	
	/**
	 * 判断竞赛类型是否重复(true为重复，false为不重复)
	 * */
	@RequestMapping(value = "/comParamTypeDC", method=RequestMethod.POST)
	public ResponseEntity<String> duplicateCheckingComParamType(
			String validationDate){
		
		if (StringUtil.isNullOrEmpty(validationDate))
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		
		JSONObject json = new JSONObject();
		json.put("state", !comParamService.CheckingComParamType(validationDate));
		
		return ResponseUtil.getResEntityForGet(json.toJSONString());
	}

    /**
	 * 通过竞赛类型得到它所有的子数据
	 * */
	@RequestMapping(value = "/getComParamType", method=RequestMethod.GET)
	public ResponseEntity<String> findChildrenByParamName(
			@PathVariable("comParamType")
			String comParamType){
		if (StringUtil.isNullOrEmpty(comParamType))
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		
		List<Map<String, Object>> comParamTables = comParamService.findChildrenByComParamType(comParamType);
		
		return ResponseUtil.getResEntityForGetAndJsonArray(comParamTables);
	}
	

	/**
	 * 通过Id数组删除竞赛
	 * */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public ResponseEntity<String> deleteTableComParam(
			@RequestParam("comParamIds")
			String[] comParamIds) {
		if(comParamIds == null || 0 == comParamIds.length)
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		
		if(comParamService.deleteTableComParamsByParamComParams(comParamIds))
			return ResponseUtil.getResEntityForPPPWhenSuccess();
		
		return ResponseUtil.getResEntityForPPPWhenFail();
	}
	
	/**
	 * 得到一条数据
	 * **/
	@RequestMapping(value = "/detail", method = RequestMethod.GET)
	public ResponseEntity<String> getComParamsByComParams( 
			@RequestParam("comParamId")
			String comParamId){
	    	System.out.println("需要查看数据的id:"+comParamId);
			if(comParamId == null)
				return ResponseUtil.getResponseEntityWhenInvalidReqParams();
	        Map<String, Object> result = comParamService.getDetailById(comParamId);

	        return ResponseUtil.getResEntityForGetAndJson(result);
	}
		
}

